# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os
import subprocess

from conans import ConanFile, tools, CMake, AutoToolsBuildEnvironment


class ProtobufConan(ConanFile):
    name = "protobuf"
    settings = "os", "compiler", "build_type", "arch"
    exports_sources = ["permissions.ini"]
    generators = "cmake_find_package_multi"
    _cmake = None
    options = {
        "shared": [True, False],
        "with_zlib": [True, False],
        "lite": [True, False]
    }
    default_options = {
        "shared": True,
        "with_zlib": False,
        "lite": False
    }
    chost = None

    @property
    def _source_subfolder(self):
        return "source_subfolder"

    def source(self):
        git = tools.Git(verify_ssl=False)
        git.clone(**self.conan_data["sources"][self.version])

    def build(self):
        self.run("sed -i 's/,no-obsolete/,no-obsolete >\/dev\/null 2>\&1/' autogen.sh")
        self.run("./autogen.sh", run_environment=True)
        args= []
        args.append('--with-pic')
        if self.settings.arch == "x86_64":
            args.append("--enable-shared")
            args.append("--disable-static")
        else:
            args.append("--disable-shared")
            args.append("--enable-static")
        if self.options.lite:
            args.append("--enable-lite-runtime")
        # 禁用前缀，保证可执行程序protoc交叉编译场景下不重命名为aarch64-linux-gnu-protoc
        args.append("--program-prefix=")
        env_build = AutoToolsBuildEnvironment(self)
        env_build.flags.append(
            " -D_FORTIFY_SOURCE=2 -Os -w"
        )
        if self.settings.build_type == "Debug":
            sysroot = os.environ.get("CONAN_CMAKE_SYSROOT")
            env_build.flags.append(f"--sysroot={sysroot}")
        if self.settings.arch == "x86_64":
            args.append("--libdir=${prefix}/lib")
        else:
            args.append("--libdir=${prefix}/usr/lib64")
        env_build.configure(args=args)
        env_build.make()
        env_build.install()


    def package(self):
        self.copy("permissions.ini", src="", dst="")

    def package_info(self):
        self.cpp_info.includedirs = ["include"]
        self.cpp_info.libdirs = ["lib"]
        self.env_info.LD_LIBRARY_PATH.append(os.path.join(self.package_folder, "lib"))
        self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.definitions["protobuf_BUILD_TESTS"] = False
        self._cmake.definitions["protobuf_BUILD_PROTOC"] = True
        self._cmake.definitions["protobuf_BUILD_LIBPROTOC"] = False
        self._cmake.definitions["protobuf_WITH_ZLIB"] = self.options.with_zlib
        self._cmake.configure()
        return self._cmake